package www.cskaoyan.com.controller;

import www.cskaoyan.com.bean.ClassManager;
import www.cskaoyan.com.bean.Dormitory;
import www.cskaoyan.com.bean.Group;
import www.cskaoyan.com.service.ClassManagerService;
import www.cskaoyan.com.service.GroupManagerService;
import www.cskaoyan.com.service.impl.ClassManagerServiceImpl;
import www.cskaoyan.com.service.impl.GroupManagerServiceImpl;
import www.cskaoyan.com.utils.Request;
import www.cskaoyan.com.utils.Response;

import java.sql.SQLException;
import java.util.List;

public class GroupManagerController {

    // 调用逻辑层
    GroupManagerService groupManagerService;
    public GroupManagerController(){
        this.groupManagerService = new GroupManagerServiceImpl();
    }


    /**
     * 获取班级列表
     * @param request:
     *  @param request:
     * @return Response(200, "成功", true)
     */
    public Response getGroupList(Request request)  {
        System.out.println("/GroupManager/getGroupList");
        System.out.println(request);
        Response response;
        List<Group> list = null;
        try {
           list =  groupManagerService.getGroupList();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null  && list.size() > 0){
            // 存在
            response = new Response(200, "分组列表", true);
            response.setData(list);
        }else {
            // 不存在
            response = new Response(0, "没有分组列表", false);
        }
        return  response;
    }


    /**
     * 创建分组
     * @param request: Request{url='GroupManager/createGroup', methods='POST', parameters={classId=5, groupName=没有bug}}
     * @param request: parameters={classId=5, groupName=没有bug}
     * @return Response(200, "成功", true)
     */
    public Response createGroup(Request request)  {
        System.out.println("/GroupManager/createGroup");
        System.out.println(request);
        Response response;
        Group group = new Group(request.getParameters());

        try {
            groupManagerService.createGroup(group);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response = new Response(200, "创建成功", true);
        return  response;
    }

    /**
     * 删除分组
     * @param request:Request{url='GroupManager/deleteGroup', methods='GET', parameters={id=1}}
     * @param request:parameters={id=1}
     * @return Response(200, "成功", true)
     */
    public Response deleteGroup(Request request)  {
        System.out.println("/GroupManager/deleteGroup");
        System.out.println(request);
        Response response;
        Group group = new Group(request.getParameters());
        try {
            response = groupManagerService.deleteGroup(group);
        } catch (SQLException e) {
            e.printStackTrace();
            response = new Response(-1, "删除失败", false);
        }

        return  response;
    }


    /**
     * 修改分组信息
     * @param request:Request{url='GroupManager/changeGroup', methods='POST', parameters={classId=2, groupName=非常帅组, id=1}}
     * @param request:parameters={classId=2, groupName=非常帅组, id=1}
     * @return Response(200, "成功", true)
     */
    public Response changeGroup(Request request)  {
        System.out.println("/GroupManager/changeGroup");
        System.out.println(request);
        Response response;
        Group group = new Group(request.getParameters());
        try {
            response = groupManagerService.changeGroup(group);
        } catch (SQLException e) {
            e.printStackTrace();
            response = new Response(-1, "修改失败", false);
        }

        return  response;
    }


}
